package service

import (
	"context"
	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/util/gconv"
	"lot_admin_api/api"
	"lot_admin_api/internal/consts"
	"lot_admin_api/internal/dao"
	"lot_admin_api/internal/model"
	"lot_admin_api/utility/paging"
	"lot_admin_api/utility/response"
)

type sRole struct{}

var (
	insRole = sRole{}
)

func Role() *sRole {
	return &insRole
}

// AddRoleReq  新增角色
func (s *sRole) AddRoleReq(ctx context.Context, req *api.AddRoleReq) (id int64, err error) {
	Company().CompanyInfo(ctx, req.CompanyId)
	saveInfo, _ := dao.LotRole.Ctx(ctx).Save(&req)
	id, _ = saveInfo.LastInsertId()
	return id, err
}

// EditRole  编辑角色
func (s *sRole) EditRole(ctx context.Context, req *api.EditRoleReq) (id int64, err error) {
	Company().CompanyInfo(ctx, req.CompanyId)
	// 查询有没有当前角色
	_, err = s.ShowRole(ctx, req.Id)
	// 更新角色信息
	_, err = dao.LotRole.Ctx(ctx).OmitEmpty().Data(&req).Where("id", req.Id).Update()
	return req.Id, err
}

// DeleteRole  删除角色
func (s *sRole) DeleteRole(ctx context.Context, req *api.DeleteRoleReq) (id int64, err error) {
	r := g.RequestFromCtx(ctx)
	// 查询有没有当前角色
	searchInfo, err := s.ShowRole(ctx, req.Id)
	// 判断员工是否使用过该角色
	if findStaffInfo, _ := dao.LotStaff.Ctx(ctx).Where(g.Map{
		"role_id": searchInfo.Id,
	}).One(); findStaffInfo != nil {
		response.JsonExit(r, consts.HTTPInternalServer.Code(), "该角色有员工,不能删除")
	}
	// 删除角色信息
	_, err = dao.LotRole.Ctx(ctx).Where("id", req.Id).Delete()
	return req.Id, err
}

// RoleInfo  获取角色详情
func (s *sRole) RoleInfo(ctx context.Context, req *api.RoleInfoReq) (res *model.RoleDetailsInfoRes, err error) {
	// 查询角色详情
	findRoleInfo, err := s.ShowRole(ctx, req.Id)
	_ = gconv.Scan(findRoleInfo, &res)
	var search *g.Map
	//组装搜索条件  当是超级管理员时查询所有权限 否则按照findRoleInfo.AuthIds 查询权限
	if findRoleInfo.IsAdmin == 1 {
		search = &g.Map{
			"id >": 0,
		}
	} else {
		search = &g.Map{
			"id": findRoleInfo.AuthIds,
		}
	}
	// 查询条件
	authInfoList, _ := dao.LotAuth.Ctx(ctx).Where(search).All()
	// 转换数据类型
	_ = gconv.Scan(authInfoList, &res.AuthInfoList)
	return res, err
}

// RoleListInfoReq  获取公司角色列表
func (s *sRole) RoleListInfoReq(ctx context.Context, req *model.RoleTerm) (res *model.RoleListPageRes, err error) {
	// 获取偏移量
	startNum, endNum := paging.Paging(req.Paging)
	roleList := make([]*model.RoleDbInfoRes, 0)
	r := g.RequestFromCtx(ctx)
	search := g.Map{
		"company_id":       req.CompanyId,
		"is_admin":         req.IsAdmin,
		"role_name like ?": "%" + req.RoleName + "%",
	}
	// 查询角色列表
	if err = dao.LotRole.Ctx(ctx).OmitEmpty().Where(search).WithAll().OrderDesc("updated_at").Limit(startNum, endNum).Scan(&roleList); err != nil {
		response.JsonExit(r, consts.HTTPInternalServer.Code(), err.Error())
	}
	// 获取总条数
	countInfo, err := dao.LotRole.Ctx(ctx).OmitEmpty().Where(search).Count()
	if err != nil {
		response.JsonExit(r, consts.HTTPInternalServer.Code(), err.Error())
	}
	putCount := paging.PutCount(req.Paging, countInfo)
	//
	res = &model.RoleListPageRes{
		List:    roleList,
		PutPage: *putCount,
	}
	return res, err
}

// ShowRole 查询当前角色是否存在
func (s *sRole) ShowRole(ctx context.Context, id int64) (searchInfo *model.RoleDbInfoRes, err error) {
	r := g.RequestFromCtx(ctx)
	//staffInfo := cache.StaffInfo(ctx)
	search := g.Map{
		"id": id,
		//"company_id": staffInfo.CompanyId,
	}
	err = dao.LotRole.Ctx(ctx).Where(search).Scan(&searchInfo)
	if searchInfo == nil {
		response.JsonExit(r, consts.HTTPInternalServer.Code(), "当前角色不存在")
	}
	return searchInfo, err
}
